package cc.eevee.jni.libsamples;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class HiSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

  private long mNativeRef;

  public HiSurfaceView(Context context) {
    super(context);
    initHiSurfaceView(context);
  }

  public HiSurfaceView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public HiSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
    this(context, attrs, defStyleAttr, 0);
  }

  public HiSurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    initHiSurfaceView(context);
  }

  private void initHiSurfaceView(Context context) {
    //getHolder().setFormat(PixelFormat.RGBA_8888);
    getHolder().addCallback(this);
  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    mNativeRef = nativeCreated(holder.getSurface());
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    nativeChanged(mNativeRef, holder.getSurface(), format, width, height);
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    nativeDestroyed(mNativeRef);
  }

  private native long nativeCreated(Surface surface);
  private native void nativeChanged(long nativeRef, Surface surface,
      int format, int width, int height);
  private native void nativeDestroyed(long nativeRef);

  static {
    try {
      System.loadLibrary("hisurfaceview");
    } catch (Exception e) {
      Log.e(HiSurfaceView.class.getSimpleName(), "load native library failed");
      e.printStackTrace();
    }
  }

}
